package hsrt.com.dynamiccomponent_android.dynamic_component;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.entity.MultiItemEntity;

import java.util.List;
import java.util.Map;

public class DynamicComponentAdapter extends BaseMultiItemQuickAdapter<MultiItemEntity, BaseViewHolder> {

    private DynamicDisplayManager dynamicDisplayManager;

    public DynamicComponentAdapter(List<MultiItemEntity> data, DynamicDisplayManager dynamicDisplayManager) {
        super(data);
        this.dynamicDisplayManager = dynamicDisplayManager;
        registerType();
    }

    @Override
    protected void convert(BaseViewHolder helper, MultiItemEntity item) {
        dynamicDisplayManager.process(helper,item,this);
    }


    private void registerType() {
        if (dynamicDisplayManager == null) {
            return;
        }
        Map<Integer, Integer> typeAndIdMap = dynamicDisplayManager.getTypeAndId();

        if (typeAndIdMap == null) {
            return;
        }

        for (Map.Entry<Integer, Integer> entry : typeAndIdMap.entrySet()) {
            addItemType(entry.getKey(), entry.getValue());
        }
    }
}
